import { Product, Category, Purchase, Supplier, Sale, SaleItem, Employee as PrismaEmployee, Prisma } from '@prisma/client'
import { Decimal } from '@prisma/client/runtime/library'

// 序列化后的商品类型
export interface SerializedProduct extends Omit<Product, 'price' | 'createdAt' | 'updatedAt'> {
  price: string
  createdAt: string
  updatedAt: string
  category: Omit<Category, 'createdAt' | 'updatedAt'> & {
    createdAt: string
    updatedAt: string
  }
}

// 序列化后的采购单类型
export interface SerializedPurchase extends Omit<Purchase, 'totalAmount' | 'createdAt' | 'updatedAt'> {
  totalAmount: string
  createdAt: string
  updatedAt: string
  supplier: Omit<Supplier, 'createdAt' | 'updatedAt'> & {
    createdAt: string
    updatedAt: string
  }
  items: Array<{
    id: number
    purchaseId: number
    productId: number
    quantity: number
    price: string
    amount: string
    createdAt: string
    updatedAt: string
    product: SerializedProduct
  }>
}

// 序列化后的销售单类型
export interface SerializedSale extends Omit<Sale, 'totalAmount' | 'createdAt' | 'updatedAt'> {
  totalAmount: string
  createdAt: string
  updatedAt: string
  items: Array<SerializedSaleItem>
}

// 序列化后的销售单明细类型
export interface SerializedSaleItem extends Omit<SaleItem, 'price' | 'amount'> {
  saleItemId: number
  price: string
  amount: string
  product: SerializedProduct
}

// 序列化后的退货单类型
export interface SerializedReturn {
  id: number
  code: string
  saleId: number
  totalAmount: string
  createdAt: string
  updatedAt: string
  sale: SerializedSale
  items: Array<{
    id: number
    returnId: number
    saleItemId: number
    productId: number
    quantity: number
    price: string
    amount: string
    reason: string
    createdAt: string
    updatedAt: string
    product: SerializedProduct
    saleItem: SerializedSaleItem
  }>
}

// 序列化后的财务记录类型
export interface SerializedFinanceRecord {
  id: number
  type: 'INCOME' | 'EXPENSE'
  amount: string
  description: string
  createdAt: string
  updatedAt: string
}

// 用户角色枚举
export enum UserRole {
  ADMIN = 'ADMIN',
  MANAGER = 'MANAGER',
  STAFF = 'STAFF'
}

// 用户类型
export interface User {
  id: number
  username: string
  password: string
  name: string
  role: UserRole
  status: boolean
  createdAt: Date
  updatedAt: Date
}

// 员工类型
export type EmployeeWithSales = Prisma.EmployeeGetPayload<{
  include: { sales: true }
}>

// 销售类型
export type SaleWithEmployee = Prisma.SaleGetPayload<{
  include: { employee: true }
}>

// 商品类型
export type ProductWithCategory = Prisma.ProductGetPayload<{
  include: { category: true }
}>

// 分类类型
export type CategoryWithProducts = Prisma.CategoryGetPayload<{
  include: { products: true }
}>

// 更新 Employee 接口
export interface Employee extends PrismaEmployee {
  SaleItem: {
    amount: {
      toNumber: () => number
    }
    sale: any
  }[]
}

// 添加商品相关类型
export interface Unit {
  id: number
  name: string
  products: Product[]
  createdAt: Date
  updatedAt: Date
}

export interface Category {
  id: number
  name: string
}

export interface Product {
  id: number
  name: string
  code: string
  price: Decimal
  stock: number
  unit: string
  categoryId: number
  category?: Category
  description?: string
  createdAt: Date
  updatedAt: Date
} 